package cn.tedu.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

//测试 反射 获取Class对象
public class Test2_ReflectStudent {
    public static void main(String[] args) throws Exception{
//        method();
//        method2();
//        method3();
//        method4();
        method5();
    }

    public static void method5() throws Exception{
        Class<Student> clazz = Student.class;
        Student s = clazz.newInstance();
        System.out.println("s = " + s);
        Constructor<Student> constructor = clazz.getConstructor(String.class);
        Student s2 = constructor.newInstance("jack");
        System.out.println("s2 = " + s2);
        Constructor<Student> constructor1 = clazz.getConstructor(String.class, int.class);
        Student s3 = constructor1.newInstance("rose", 81);
        System.out.println("s3 = " + s3);
    }

    public static void method4() {
        Class<Student> clazz = Student.class;
        Field[] fs =  clazz.getFields();
        for (Field f : fs) {
            System.out.println(f.getName());
            System.out.println(f.getType().getName());
        }
    }

    public static void method3() {
        Class<Student> clazz = Student.class;
        //获取所有成员方法们--包括自己的和父类的
        Method[] ms = clazz.getMethods();
        for (Method m : ms) {
            System.out.println(m.getName());
            Class[] cs = m.getParameterTypes();
            System.out.println(Arrays.toString(cs));
        }
    }

    public static void method2() {
        Class clazz = Student.class;
        Constructor[] cs = clazz.getConstructors();
        for (Constructor c : cs) {
            System.out.println(c.getName());
            Class[] css = c.getParameterTypes();
            System.out.println(Arrays.toString(css));
        }
//        System.out.println(Arrays.toString(cs));
    }

    public static void method() throws ClassNotFoundException {
        Class clazz1 = Class.forName("cn.tedu.reflect.Student");
        Class clazz2 = Student.class;
        Class clazz3 = new Student().getClass();
        System.out.println("clazz3 = " + clazz3);
        System.out.println("clazz2 = " + clazz2);
        System.out.println("clazz1 = " + clazz1);
    }
}
